package com.team.panicgame.models.gareve;

import com.badlogic.gdx.math.Vector2;
import com.team.panicgame.GameConstants;
import com.team.panicgame.Utils;
import com.team.panicgame.models.LivingGameObject;

public class Ship extends LivingGameObject{
	public static final float WIDTH = 5f;
	public static final float HEIGHT = 5f;
	
	public Vector2 direction;//a unitary vector
	
	
	//helpers
	private Vector2 center;
	public Vector2 p1,p2,p3;
	
	public Ship(float x, float y) {
		super(x, y, WIDTH,HEIGHT);
		direction = new Vector2(0, -1f);
		
		refreshPosition();
		
	}
	
	private void refreshPosition(){
		center = new Vector2(shape.x + WIDTH / 2f, shape.y + HEIGHT);
		
		p1 = Utils.rotateAround(new Vector2(shape.x,shape.y),center,getAngle());
		p2 = Utils.rotateAround(new Vector2(shape.x+Ship.WIDTH,shape.y),center,getAngle());
		p3 = Utils.rotateAround(new Vector2(shape.x+Ship.WIDTH/2f,shape.y + Ship.HEIGHT * 2f),center,getAngle());
	}
	
	public void move(float delta,Vector2 moveVector){
		if(moveVector.x != 0f || moveVector.y != 0f){//There is some user input
			shape.x += moveVector.x * GameConstants.SHIP_SPEED_CONSTANT;
			shape.y += moveVector.y * GameConstants.SHIP_SPEED_CONSTANT;			
			
			if(moveVector.x == 0f)
				direction.x *= 0.8f;
			else
				direction.x += GameConstants.SHIP_ROTATE_CONSTANT * moveVector.x;
			
			if(moveVector.y == 0f)
				direction.y *= 0.8f;
			else
				direction.y += GameConstants.SHIP_ROTATE_CONSTANT * moveVector.y;
			
			direction.x = Math.min(1,Math.max(-1f,direction.x));//[-1,1]
			direction.y = Math.min(1,Math.max(-1f,direction.y));//[-1,1]
			
			refreshPosition();
		}
	}

	public float getAngle() {
		float angle = direction.angle() - 90f;
		
		return angle;
	}
}
